package com.hgk.wanwan.main.fragment.persentfragment.handeredit;

import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.hgk.common.common.ui.activity.BallTitleAcitivty;

import java.util.ArrayList;
import java.util.List;

import width.hgk.wanwan.R;

public class AddImageActivity extends BallTitleAcitivty implements ColorPickerDialog.OnColorChangedListener {

    private Paint mPaint;
    private MaskFilter mEmboss;
    private MaskFilter mBlur;
    private List<Bitmap> mBitmaps = new ArrayList<Bitmap>();
    private Bitmap mBitmap;
    private Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_add_image);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFF000000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(25);
        mEmboss = new EmbossMaskFilter(new float[]
                { 1, 1, 1 }, 0.4f, 6, 3.5f);
        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
        MyView myView = (MyView) findViewById(R.id.myView1);
        myView.setPaint(mPaint);
        myView.setBitmapMap(mBitmaps);
        gridView = (GridView) findViewById(R.id.gridView1);
        adapter = new MyBaseAdapter(this, mBitmaps);
        gridView.setAdapter(adapter);
        Button button = (Button) findViewById(R.id.deletebutton);
        Button mBtnSave = (Button) findViewById(R.id.savebutton);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                int index = mBitmaps.size() - 1;
                if (!mBitmaps.isEmpty())
                {
                    mBitmaps.remove(index);
                    test();
                }
            }
        });
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                new Thread() {
//                    public void run() {
//                        while (isStart) {
//                            long time = System.currentTimeMillis();
//                            if (time >= mEndTime + 1000) {
//                                if (mBitmaps != null) {
//                                    int width = mBitmap.getWidth();
//                                    int height = mBitmap.getHeight();
//                                    Matrix matrix = new Matrix();
//                                    float scaleWidht = ((float) 60 / width);
//                                    float scaleHeight = ((float) 80 / height);
//                                    matrix.postScale(scaleWidht, scaleHeight);
//                                    Bitmap newbmp = Bitmap.createBitmap(mBitmap, 0, 0, width, height,
//                                            matrix, true);
//                                    mBitmaps.add(newbmp);
//                                }
//                                mHandler.sendEmptyMessage(0);
//                                isStart = false;
//                            }
//                        }
//                    };
//                }.start();
            }
        });
    }
    public void test()
    {
        gridView.smoothScrollToPosition(mBitmaps.size());
        adapter.notifyDataSetChanged();
    }

    private static final int COLOR_MENU_ID = Menu.FIRST;
    private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
    private static final int BLUR_MENU_ID = Menu.FIRST + 2;
    private static final int ERASE_MENU_ID = Menu.FIRST + 3;
    private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;
    private MyBaseAdapter adapter;
    private GridView gridView;

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);

        menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
        menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
        menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
        menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
        menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z');

        /****
         * Is this the mechanism to extend with filter effects? Intent intent =
         * new Intent(null, getIntent().getData());
         * intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
         * menu.addIntentOptions( Menu.ALTERNATIVE, 0, new ComponentName(this,
         * NotesList.class), null, intent, 0, null);
         *****/
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
        super.onPrepareOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        mPaint.setXfermode(null);
        mPaint.setAlpha(0xFF);

        switch (item.getItemId())
        {
            case COLOR_MENU_ID:
                new ColorPickerDialog(this, this, mPaint.getColor()).show();
                return true;
            case EMBOSS_MENU_ID:
                if (mPaint.getMaskFilter() != mEmboss)
                {
                    mPaint.setMaskFilter(mEmboss);
                }
                else
                {
                    mPaint.setMaskFilter(null);
                }
                return true;
            case BLUR_MENU_ID:
                if (mPaint.getMaskFilter() != mBlur)
                {
                    mPaint.setMaskFilter(mBlur);
                }
                else
                {
                    mPaint.setMaskFilter(null);
                }
                return true;
            case ERASE_MENU_ID:
                mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                return true;
            case SRCATOP_MENU_ID:
                mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
                mPaint.setAlpha(0x80);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void colorChanged(int color)
    {
        // TODO Auto-generated method stub
        mPaint.setColor(color);
    }

    @Override
    public void setLeftButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setRightButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public Object getTitleString() {
        return "签名页";
    }

    @Override
    public int getBodyView() {
        return R.layout.activity_add_image;
    }
}
